package cn.hg.solon.youcan.framework.provider;

import java.util.ArrayList;
import java.util.List;

import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.text.CharSequenceUtil;
import org.dromara.hutool.core.text.StrValidator;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.handle.MethodType;

import cn.hg.solon.youcan.common.util.ContextPathUtil;
import cn.hg.solon.youcan.common.web.PearConfig;
import cn.hg.solon.youcan.common.web.PearConfig.Color;
import cn.hg.solon.youcan.framework.service.PearService;
import cn.hg.solon.youcan.common.web.PearMenuItem;
import cn.hg.solon.youcan.system.entity.SysConfig;
import cn.hg.solon.youcan.system.entity.SysPermission;
import cn.hg.solon.youcan.system.entity.SysUser;
import cn.hg.solon.youcan.system.service.SysConfigService;
import cn.hg.solon.youcan.system.service.SysPermissionService;

/**
 * @author 胡高
 */
@Component
public class PearProvider implements PearService {

	private static String VIEW_BASE = "/admin/";

	@Inject
	private SysConfigService configService;

	@Inject
	private SysPermissionService permissionService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.hg.solon.youcan.web.pear.PearService#buildMenu(cn.hg.solon.youcan.system.
	 * entity.SysUser, java.lang.Integer[])
	 */
	@Override
	public List<PearMenuItem> buildMenu(SysUser user, Integer[] rootIds) {

		List<SysPermission> menuList = this.permissionService.listByUser(user);
		List<PearMenuItem> list = new ArrayList<>();

		for (SysPermission menu : menuList) {
			if (ArrayUtil.contains(rootIds, menu.getParentId())) {
				PearMenuItem item = new PearMenuItem();
				item.setId(menu.getId());
				item.setTitle(menu.getName());
				item.setHref(ContextPathUtil.prefixContextPath(menu.getUrl()));
				if (StrValidator.isNotBlank(menu.getTarget())) {
					item.setOpenType(menu.getTarget());
				}
				item.setIcon("layui-icon " + menu.getIcon());
				List<PearMenuItem> children = this.buildChildrenMenuJsonObject(item, menuList);
				if (CollUtil.isNotEmpty(children)) {
					item.setType(0);
					item.setChildren(children);
				} else {
					item.setType(1);
				}
				item.setOpenType(menu.getTarget());

				list.add(item);
			}
		}
		return list;
	}

	private List<PearMenuItem> buildChildrenMenuJsonObject(PearMenuItem parent, List<SysPermission> menuList) {
		int parentId = parent.getId();

		List<PearMenuItem> jsonList = new ArrayList<>();

		for (SysPermission menu : menuList) {
			if (!CharSequenceUtil.equals("MENU", menu.getType()) || !(parentId == menu.getParentId().intValue())) {
				continue;
			}

			/*
			 * 示例 { "id": 20, "title":
			 * "<i class='layui-icon layui-icon-app'></i>&nbsp;&nbsp;系统用户", "icon":
			 * "layui-icon layui-icon-util", "type": 0, "href": "", "children": [ }
			 */
			PearMenuItem item = new PearMenuItem();
			item.setId(menu.getId());
			item.setTitle(menu.getName());
			item.setHref(ContextPathUtil.prefixContextPath(menu.getUrl()));
			item.setIcon("layui-icon " + menu.getIcon());
			List<PearMenuItem> children = this.buildChildrenMenuJsonObject(item, menuList);
			item.setChildren(children);
			item.setOpenType(menu.getTarget());
			item.setType(CollUtil.isEmpty(children) ? 1 : 0);

			jsonList.add(item);
		}
		return jsonList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.hg.solon.youcan.web.pear.PearService#getConfig()
	 */
	@Override
	public PearConfig getConfig() {
		PearConfig config = new PearConfig();

		/*
		 * logo
		 */
		SysConfig icon = this.configService.getByTypeAndCode("SITE", "icon");
		SysConfig fullname = this.configService.getByTypeAndCode("SITE", "fullname");

		config.getLogo().setTitle("<div style='cursor:pointer;'><img class='logo' src='" + icon.getValue() + "'>&nbsp;"
				+ fullname.getValue() + "</div>");
		config.getLogo().setImage(icon.getValue());

		/*
		 * menu
		 */
		config.getMenu().setData(VIEW_BASE + "menu?ids=1");
		config.getMenu().setCollaspe(false);
		config.getMenu().setAccordion(true);
		config.getMenu().setMethod(MethodType.GET.name);
		config.getMenu().setControl(false);
		config.getMenu().setSelect("0");
		config.getMenu().setAsync(true);

		/*
		 * tab
		 */
		config.getTab().setEnable(true);
		config.getTab().setKeepState(true);
		config.getTab().setMax(10);
		config.getTab().setSession(false);
		config.getTab().setPreload(false);
		config.getTab().getIndex().setHref(VIEW_BASE + "home");
		config.getTab().getIndex().setId("2");
		config.getTab().getIndex().setTitle("<i class='layui-icon layui-icon-home'></i>&nbsp;首页");

		/*
		 * theme
		 */
		config.getTheme().setDefaultColor(1);
		config.getTheme().setDefaultHeader("light-theme");
		config.getTheme().setDefaultMenu("dark-theme");

		/*
		 * colors
		 */
		Color color1 = new Color();
		color1.setId(1);
		color1.setColor("#2d8cf0");
		color1.setSecond("#ecf5ff");
		config.getColors().add(color1);

		Color color2 = new Color();
		color2.setId(2);
		color2.setColor("#36b368");
		color2.setSecond("#f0f9eb");
		config.getColors().add(color2);

		Color color3 = new Color();
		color3.setId(3);
		color3.setColor("#f6ad55");
		color3.setSecond("#fdf6ec");
		config.getColors().add(color3);

		Color color4 = new Color();
		color4.setId(4);
		color4.setColor("#f56c6c");
		color4.setSecond("#fef0f0");
		config.getColors().add(color4);

		Color color5 = new Color();
		color5.setId(5);
		color5.setColor("#3963bc");
		color5.setSecond("#ecf5ff");
		config.getColors().add(color5);

		/*
		 * other
		 */
		config.getOther().setAutoHead(true);
		config.getOther().setKeepLoad(100);
		config.getOther().setFooter(true);

		/*
		 * header
		 */
		config.getHeader().setMessage(false);

		return config;
	}
}
